﻿//$(function () {
//    $("#txt_fdate").datepicker({ dateFormat: 'dd/mm/yy' });
//    $("#txt_tdate").datepicker({ dateFormat: 'dd/mm/yy' });
//});

function ClientSideGetReportTable() {
//    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
//    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var FromDate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var ToDate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var LocId = $("#txt_tdate").val();
    var LocLevel = $("#txt_tdate").val();
    var RowType = $("#DrpSelectRow").val();
    var MainColName = $("#DrpSelectRow option:selected").text()
    var tableString = Portal.SODC.IDIS.DrugInformation.ServerSideRenderDrugReportTable(FromDate, ToDate, RowType, MainColName, LocId, LocLevel).value;

    if (tableString.indexOf("ERROR") != -1) {
        alert(tableString.replace("ERROR:", "").trim());
        return;
    }
    $("#divStatisticResult").html(tableString);
}

function fireParentForm(LocationType, WebpartIdPermission, PermissionCode) {
}

function ExecuteAdvSearch() {
    //document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    if (CheckValidData() == false) return;
    SearchData = "";

    //doan nay lay du lieu de bind vao cau lenh tim kiem....!
    //lay xem co bao nhieu field tat ca
    MaxFields = document.getElementById("txtMaxSearchFields").value;
    for (var IndexField = 0; IndexField < MaxFields; IndexField++) {
        ValueText = "";
        FieldId = document.getElementById("txtFieldId" + IndexField + "").value;
        FieldType = document.getElementById("txtFieldType" + FieldId + "").value;
        FieldTable = document.getElementById("txtFieldTable" + FieldId + "").value;
        FieldName = document.getElementById("txtFieldName" + FieldId + "").value;
        FieldSearchType = document.getElementById("txtFieldSearchType" + FieldId + "").value;
        FieldRequired = document.getElementById("txtFieldRequired" + FieldId + "").value;
        FieldDisplay = document.getElementById("txtFieldDisplay" + FieldId + "").value;
        if (FieldType == 'text' || FieldType == 'int' || FieldType == 'decimal') {
            ValueText = document.getElementById("txtField" + FieldId + "").value;
        }
        if (FieldType == 'combobox' || FieldType == 'dictionary') {
            SelectedIndex = document.getElementById("drpField" + FieldId + "").selectedIndex;
            ValueText = document.getElementById("drpField" + FieldId + "").options[SelectedIndex].value;
        }

        SearchData = SearchData + FieldId + ":" + FieldType + ":" + FieldTable + ":" + FieldName + ":" + FieldSearchType + ":" + FieldDisplay + ":" + FieldRequired + ":" + ValueText;
        if (IndexField < MaxFields - 1) {
            SearchData = SearchData + ";";
        }
    }

//    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
//    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var FromDate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var ToDate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var LocId = $("#_locationsel").val();
    var LocLevel = $("#_locationLevelTypeSelect").val(); 
    var RowType = $("#DrpSelectRow").val();
    var MainColName = $("#DrpSelectRow option:selected").text()
    var rValue = Portal.SODC.IDIS.DrugInformation.ServerSideRenderDrugReportTable(FromDate, ToDate, SearchData, RowType, MainColName, LocId, LocLevel).value;
    document.getElementById("divProcessing").innerHTML = "";
    if (rValue.indexOf("ERROR") != -1) {
        
        alert(rValue.replace("ERROR:", "").trim());
        return;
    }
    $("#divStatisticResult").html(rValue);
}

/* Old script */

function selectLevelTypeForReport() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.DrugInformation.ServerSideRenderLocationSelect(ltype).value);
}

function showhide(a, id) {
    var e = document.getElementById(id);
    if (e.style.display == 'none') {
        e.style.display = 'block';
        a.innerHTML = 'Ẩn tìm kiếm nâng cao';
    }
    else {
        e.style.display = 'none';
        a.innerHTML = 'Tìm kiếm nâng cao';
    }
}

function ClientMainExportData() {
    //document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    if (CheckValidData() == false) return;
    SearchData = "";

    //doan nay lay du lieu de bind vao cau lenh tim kiem....!
    //lay xem co bao nhieu field tat ca
    MaxFields = document.getElementById("txtMaxSearchFields").value;
    for (var IndexField = 0; IndexField < MaxFields; IndexField++) {
        ValueText = "";
        FieldId = document.getElementById("txtFieldId" + IndexField + "").value;
        FieldType = document.getElementById("txtFieldType" + FieldId + "").value;
        FieldTable = document.getElementById("txtFieldTable" + FieldId + "").value;
        FieldName = document.getElementById("txtFieldName" + FieldId + "").value;
        FieldSearchType = document.getElementById("txtFieldSearchType" + FieldId + "").value;
        FieldRequired = document.getElementById("txtFieldRequired" + FieldId + "").value;
        FieldDisplay = document.getElementById("txtFieldDisplay" + FieldId + "").value;
        if (FieldType == 'text' || FieldType == 'int' || FieldType == 'decimal') {
            ValueText = document.getElementById("txtField" + FieldId + "").value;
        }
        if (FieldType == 'combobox' || FieldType == 'dictionary') {
            SelectedIndex = document.getElementById("drpField" + FieldId + "").selectedIndex;
            ValueText = document.getElementById("drpField" + FieldId + "").options[SelectedIndex].value;
        }

        SearchData = SearchData + FieldId + ":" + FieldType + ":" + FieldTable + ":" + FieldName + ":" + FieldSearchType + ":" + FieldDisplay + ":" + FieldRequired + ":" + ValueText;
        if (IndexField < MaxFields - 1) {
            SearchData = SearchData + ";";
        }
    }

    var FromDate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var ToDate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    var FromDate = $("#txt_fdate").val();
//    var ToDate = $("#txt_tdate").val();
    var LocId = $("#_locationsel").val();
    var LocLevel = $("#_locationLevelTypeSelect").val();
    var RowType = $("#DrpSelectRow").val();
    var MainColName = $("#DrpSelectRow option:selected").text()
    var rValue = Portal.SODC.IDIS.DrugInformation.ServerSideRenderExcel(FromDate, ToDate, SearchData, RowType, MainColName, LocId, LocLevel).value;
    document.getElementById("divProcessing").innerHTML = "";
    if (rValue.indexOf('ERROR') != -1) {
        alert(rValue.replace("ERROR:", "").trim());
        //alert('Lỗi trong quá trình xử lý:' + rValue);
    }
    else {
        window.open(rValue, '_blank');
    }
//    document.getElementById("divProcessing").innerHTML = 'Đang xử lý xin vui lòng chờ ....';
//    setTimeout("RealClientMainExportData()", 10);
}
function CheckValidData() {
    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    fromyear = document.getElementById("txt_fdate").value;
//    toyear = document.getElementById("txt_tdate").value;
    if (fromyear.length == 0 || toyear == 0) {
        alert("Bạn phải chọn thời gian để tạo thống kê!")
        return false;
    }

    var vallocation = $("#selectlevel").val();

    var sublocation = $("#_locationsel").val();
    if (vallocation == 0 || sublocation == 0) {
        alert("Bạn phải chọn cấp - địa phương để xuất dữ liệu !")
        return false;
    }

    ColIndex = document.getElementById("DrpSelectCol").selectedIndex;
    ColVal = document.getElementById("DrpSelectCol").options[ColIndex].value;

    RowIndex = document.getElementById("DrpSelectRow").selectedIndex;
    RowVal = document.getElementById("DrpSelectRow").options[RowIndex].value;
    if (RowVal == ColVal) {
        alert('Giá trị Cột và Hàng phải khác nhau.');
        return false;
    }

    if (ColVal == 'Year') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year == tyear) {
            alert('Thống kê năm không thực hiện với khoảng thời gian trong một năm.');
            return false;
        }
    }

    if (ColVal == 'Month') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year != tyear) {
            alert('Thống kê tháng chỉ thực hiện với khoảng thời gian trong một năm.');
            return false;
        }
    }
    return true;
}
function RealClientMainExportData() {
    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    fromyear = document.getElementById("txt_fdate").value;
//    toyear = document.getElementById("txt_tdate").value;
    if (fromyear == "//" || toyear == "//") {
        alert("Bạn phải chọn thời gian để tạo thống kê!")
        return;
    }

    var vallocation = $("#selectlevel").val();

    var sublocation = $("#_locationsel").val();
    if (sublocation.length == 0 || sublocation == 0) {
        alert("Bạn phải chọn cấp - địa phương để xuất dữ liệu !")
        return;
    }

    ColIndex = document.getElementById("DrpSelectCol").selectedIndex;
    ColVal = document.getElementById("DrpSelectCol").options[ColIndex].value;

    RowIndex = document.getElementById("DrpSelectRow").selectedIndex;
    RowVal = document.getElementById("DrpSelectRow").options[RowIndex].value;
    if (RowVal == ColVal) {
        alert('Giá trị Cột và Hàng phải khác nhau.');
        return;
    }

    if (ColVal == 'Year') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year == tyear) {
            alert('Thống kê năm không thực hiện được với khoảng thời gian trong một năm.');
            return;
        }
    }

    if (ColVal == 'Month') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year != tyear) {
            alert('Thống kê tháng chỉ thực hiện được với khoảng thời gian trong một năm.');
            return;
        }
    }

    genderIndex = document.getElementById("cbgender").selectedIndex;
    gender = document.getElementById("cbgender").options[genderIndex].value;

    ageIndex = document.getElementById("cbage").selectedIndex;
    age = document.getElementById("cbage").options[ageIndex].value;

    ethnicIndex = document.getElementById("cbethnic").selectedIndex;
    ethnic = document.getElementById("cbethnic").options[ethnicIndex].value;

    nationalityIndex = document.getElementById("cbnationality").selectedIndex;
    nationality = document.getElementById("cbnationality").options[nationalityIndex].value;

    regionIndex = document.getElementById("cbregion").selectedIndex;
    region = document.getElementById("cbregion").options[regionIndex].value;

    drugstatusIndex = document.getElementById("cbdrugstatus").selectedIndex;
    drugstatus = document.getElementById("cbdrugstatus").options[drugstatusIndex].value;

    culturalIndex = document.getElementById("cbcultural").selectedIndex;
    druglevel = document.getElementById("cbcultural").options[culturalIndex].value;

    homememberIndex = document.getElementById("cbhomemember").selectedIndex;
    homemember = document.getElementById("cbhomemember").options[homememberIndex].value;

    usedrugIndex = document.getElementById("cbusedrug").selectedIndex;
    usedrug = document.getElementById("cbusedrug").options[usedrugIndex].value;

    drugtypeIndex = document.getElementById("cbdrugtype").selectedIndex;
    drugtype = document.getElementById("cbdrugtype").options[drugtypeIndex].value;

    drugdeadIndex = document.getElementById("cbdeadtype").selectedIndex;
    drugdead = document.getElementById("cbdeadtype").options[drugdeadIndex].value;

    var rValue = Portal.SODC.IDIS.DrugInformation.ServerExportData(ColName, RowName, fromyear, toyear, vallocation, sublocation, gender, age, region, nationality, ethnic, drugstatus, druglevel, homemember, usedrug, drugtype, drugdead).value;
    document.getElementById("divProcessing").innerHTML = '';
    if (rValue.indexOf('ERROR') != -1) {
        alert(rValue.replace("ERROR:", "").trim());
        //alert('Lỗi trong quá trình xử lý:' + rValue);
    }
    else {
        window.open(rValue, '_blank');
    }
}

function ClientDoView() {
    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    fromyear = document.getElementById("txt_fdate").value;
//    toyear = document.getElementById("txt_tdate").value;
    if (fromyear == "//" || toyear == "//") {
        alert("Bạn phải chọn thời gian để tạo thống kê!")
        return;
    }

    var vallocation = $("#selectlevel").val();

    var sublocation = $("#_locationsel").val();
    if (vallocation == 0 || sublocation == 0) {
        alert("Bạn phải chọn cấp - địa phương để xuất dữ liệu !")
        return;
    }

    ColIndex = document.getElementById("DrpSelectCol").selectedIndex;
    ColVal = document.getElementById("DrpSelectCol").options[ColIndex].value;

    RowIndex = document.getElementById("DrpSelectRow").selectedIndex;
    RowVal = document.getElementById("DrpSelectRow").options[RowIndex].value;
    if (RowVal == ColVal) {
        alert('Giá trị Cột và Hàng phải khác nhau.');
        return;
    }

    if (ColVal == 'Year') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year == tyear) {
            alert('Thống kê năm không thực hiện với khoảng thời gian trong một năm.');
            return;
        }
    }

    if (ColVal == 'Month') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year != tyear) {
            alert('Thống kê tháng chỉ thực hiện với khoảng thời gian trong một năm.');
            return;
        }
    }

    document.getElementById("divProcessing").innerHTML = 'Đang xử lý xin vui lòng chờ ....';
    setTimeout("ClientRealDoView()", 10);
}
